package com.example.demo01.Service.wenSocketService;

import com.alibaba.fastjson.JSON;
import com.example.demo01.Entity.webEntity.WarnRecordEntity;
import com.example.demo01.Entity.websocketEntity.WebSocket;
import com.example.demo01.Service.mobileService.GeoCodeService;
import com.example.demo01.utils.DateParseUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@RestController
public class WebSocketService {
    //自动注入
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private JdbcTemplate jdbcTemplate = null;

    /**
     * 向Web端推送新的报警信息
     * @return
     */
    @RequestMapping(value = "/createwarnrecord")
    public String test(HttpServletRequest request, HttpServletResponse response) {
//        System.out.println(idnum+"\t"+lon+"\t"+lat);
        //获取请求参数
        String idnum = request.getParameter("idnum");
        String lon = request.getParameter("lon");
        String lat = request.getParameter("lat");
        System.out.println(idnum + "\t" + lon + "\t" + lat);
        //根据请求参数执行方法
        String recordMsg = createNewWarnRecord(idnum, lon, lat);
        webSocket.sendMessage(recordMsg);
        return "delete success";
    }

    /**
     * 根据身份证号、经纬度信息执行数据更新、消息推送
     * @param idnum
     * @param lon
     * @param lat
     * @return
     */
    public String createNewWarnRecord(String idnum, String lon, String lat) {
        //SQL语句
        String sqlInsert = "INSERT INTO warnrecord(idnumber,wlon,wlat,warntime,status,warnaddress) VALUES(?,?,?,?,0,?)";
        String sqlSelect = "SELECT id,idnumber,name,wlon,wlat,warntime,status,adminaccount,endtime,warnaddress\n" +
                "FROM warnrecord INNER JOIN userinfo1 ON userinfo1.idnum=warnrecord.idnumber\n" +
                "ORDER BY warntime DESC\n" +
                "LIMIT 1";
        //调用WebService服务，执行反向地理编码，获取详细位置信息
        String warnaddress = new GeoCodeService().getLocationByLngLat(lon, lat);
        //调用日期转换工具类，获取当前时间对应的字符串
        String curDateStr = DateParseUtils.getDateStrByFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        //1-数据库记录插入操作
        int update = jdbcTemplate.update(sqlInsert, new Object[]{idnum, lon, lat, curDateStr, warnaddress});
        System.out.println(update);
        //2-WarnRecord对象查询操作,获取最新一条记录
        List<WarnRecordEntity> warnRecordEntities = jdbcTemplate.query(sqlSelect, new RowMapper<WarnRecordEntity>() {
            @Nullable
            @Override
            public WarnRecordEntity mapRow(ResultSet resultSet, int i) throws SQLException {
                return new WarnRecordEntity(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getFloat(4),
                        resultSet.getFloat(5),
                        resultSet.getString(6),
                        resultSet.getInt(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10)
                );
            }
        });
        //返回标志给消息发送者
        return JSON.toJSONString(warnRecordEntities);
    }


    //客户端发送request请求，出发以下方法
    @RequestMapping("/createorder")
    public String createOrder(HttpServletRequest request, HttpServletResponse response) {
        //获取cookie中的订单号
        webSocket.sendMessage("有新的订单");
        return "success";
    }


    @RequestMapping("/deleteorder")
    public String deleOrder(HttpServletRequest request, HttpServletResponse response) {
        //删除订单
        webSocket.sendMessage("删除订单");
        return "delete success";
    }

    @Test
    public void test1() {
        System.out.println();
    }


}
